from playwright.sync_api import Playwright, sync_playwright, expect
import openpyxl
import time


book = openpyxl.load_workbook('/Users/zhangwenchao/PycharmProjects/PythonQuickStart/playwright/sj.xlsx')
ws = book['Sheet1']

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("http://106.120.181.196/cpad/main")
    page.goto(
        "http://106.38.235.201:7080/cas/login?service=http%3A%2F%2F106.38.235.201%3A7080%2Fportal%2Fj_spring_cas_security_check%3Bjsessionid%3D0j58HEOSUs44iEqkuNV_pFcx6sf3eYvRbZ9MgAKRSwvVQZvB7SFr%211512771193")
    page.locator("#login-content-lb").click()
    page.locator("#username").click()
    page.locator("#username").fill("41142401001")
    page.locator("#login-content-lb").click()
    page.locator("#password").click()
    page.locator("#password").fill("zfl1.A")
    #dddorc

    page.get_by_role("link", name="乡村建设信息监测平台").click()
    page.get_by_role("link", name="电信地址：http://106.120.181.196/cpad/loginXCJS").click()
    page.get_by_role("link", name="信息采集 ").click()
    page.get_by_role("link", name=" 动态管理").click()
    page.get_by_role("link", name=" 2022年度").click()
    page.get_by_role("link", name=" 农户信息采集").click()

    for a in range(ws.max_row):
        xzc = ws.cell(row=4, column=1).value
        zrc = ws.cell(row=4, column=2).value
        xm = ws.cell(row=4, column=3).value
        dh = ws.cell(row=4, column=4).value
        sfz = ws.cell(row=4, column=5).value
        rk = ws.cell(row=4, column=6).value
        mz = ws.cell(row=4, column=7).value
        mm = ws.cell(row=4, column=8).value

        page.get_by_role("tabpanel", name="农户信息采集").get_by_role("textbox").click()
        page.get_by_role("tabpanel", name="农户信息采集").get_by_role("textbox").fill(sfz)
        page.get_by_role("button", name="查询").click()
        yz = page.get_by_text("已存在").inner_html(timeout=0)
        ap = page.get_by_text("填写").inner_html(timeout=0)

        print(yz)
        if yz:
            if ap:
                page.get_by_role("button", name="确定").click()
                shuru(page)
            else:
                page.get_by_role("button", name="确定").click()
                ws.delete_rows(4, 1)
                book.save(filename='D:\\Desktop\\sj.xlsx')
        else:
            page.get_by_role("textbox", name="不超过25个汉字").click()
            page.get_by_role("textbox", name="不超过25个汉字").fill(xm)
            page.locator("#AAR006").get_by_text("全选").click()
            page.get_by_role("listitem").filter(has_text=xzc).click()
            page.locator("#aab007").get_by_text("无").click()
            page.get_by_role("listitem").filter(has_text=mz).click()
            page.locator("#aak033").get_by_text("无").click()
            page.get_by_role("listitem").filter(has_text=mm).click()
            page.locator("#aac017").click()
            page.locator("#aac017").fill(rk)
            page.locator("#aar012").click()
            page.locator("#aar012").fill(dh)
            shuru()


    # ---------------------
    context.close()
    browser.close()

def shuru(page):

    dl = ws.cell(row=4, column=9).value
    zls = ws.cell(row=4, column=10).value
    ny = ws.cell(row=4, column=13).value
    lx = ws.cell(row=4, column=14).value
    wf = ws.cell(row=4, column=15).value
    jh = ws.cell(row=4, column=16).value
    ly = ws.cell(row=4, column=17).value
    cs = ws.cell(row=4, column=18).value
    cslx = ws.cell(row=4, column=19).value
    sy = ws.cell(row=4, column=20).value
    rs = ws.cell(row=4, column=21).value
    ry = ws.cell(row=4, column=22).value
    mh = ws.cell(row=4, column=23).value
    ny = ws.cell(row=4, column=13).value
    qt = ws.cell(row=4, column=24).value
    # 入户路
    page.locator("#aac340").get_by_text("无").click()
    page.get_by_role("listitem").filter(has_text=dl).click()

    # 自来水
    if zls == '是':
        page.locator("#aac341").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text=zls).click()
        page.locator("#aac342").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="是").click()
        page.locator("#aac091").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="是").click()
    else:
        page.locator("#aac341").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="否").click()
        page.locator("#aac091").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="是").click()

    # 能源
    if ny == '是':
        page.locator("#aac343").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="是").click()
        page.get_by_title("#344").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text=lx).locator("span").click()
        if lx == "其他":
            page.locator("#aac345").click()
            page.locator("#aac345").fill(qt)

    else:
        page.locator("#aac343").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="否").click()

    # 危房
    if wf == '否':
        page.locator("#aac318").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="否").click()
    else:
        page.locator("#aac318").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="是").click()
        page.locator("#aac346").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text=jh).click()

    page.locator("#aac347").get_by_text("无").click()
    page.get_by_role("listitem").filter(has_text=ly).click()

    if cs == '是':
        # 卫生户厕
        page.locator("#aac319").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="是").click()
        page.locator("#aac348").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text=cslx).click()
        page.locator("#aac349").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text=sy).click()
        if rs == '是':
            # 入室和入户一个是一个否
            page.locator("#aac350").get_by_text("无").click()
            page.get_by_role("listitem").filter(has_text="是").click()
            page.locator("#aac351").get_by_text("无").click()
            page.get_by_role("listitem").filter(has_text="否").click()
        else:
            page.locator("#aac350").get_by_text("无").click()
            page.get_by_role("listitem").filter(has_text="否").click()
            page.locator("#aac351").get_by_text("无").click()
            page.get_by_role("listitem").filter(has_text="是").click()
    else:
        page.locator("#aac319").get_by_text("无").click()
        page.get_by_role("listitem").filter(has_text="否").click()

    # 庭院美化
    page.locator("#aac352").get_by_text("无").click()
    page.get_by_role("listitem").filter(has_text=mh).click()

    ws.delete_rows(4, 1)
    book.save(filename='D:\\Desktop\\sj.xlsx')

    time.sleep(10)
    page.get_by_role("button", name="保存").click()
    page.get_by_role("button", name="确定").click()

with sync_playwright() as playwright:

    run(playwright)
